# -*- coding: utf-8 -*-

from . import abstract_report_xlsx
from odoo.report import report_sxw
from odoo import _


class InvoiceItemsCustomerXslx(abstract_report_xlsx.AbstractReportXslx):

    def __init__(self, name, table, rml=False, parser=False, header=True,
                 store=False):
        super(InvoiceItemsCustomerXslx, self).__init__(
            name, table, rml, parser, header, store)

    def _get_report_name(self):
        return _('Customer Invoice Items')

    def _get_report_columns(self, report):
        return {
            0: {'header': _('Invoice'), 'field': 'invoice_name', 'width': 15},
            1: {'header': _('SO'), 'field': 'so', 'width': 15},
            2: {'header': _('Customer SO'), 'field': 'customer_so', 'width': 15},
            3: {'header': _('Product Name'), 'field': 'product_name', 'width': 40},
            4: {'header': _('Delivery QTY'),
                'field': 'delivered_qty',
                'type': 'amount',
                'width': 14},
            5: {'header': _('UOM'), 'field': 'uom', 'width': 12},
            6: {'header': _('Cur.'), 'field': 'curr_name', 'width': 7},
            7: {'header': _('Unit Price'),
                'field': 'price_unit',
                'type': 'amount',
                'width': 14},
            8: {'header': _('Amount'),
                'field': 'amount',
                'type': 'amount',
                'width': 14},
        }

    def _get_report_filters(self, report):
        return []

    def _get_col_count_filter_name(self):
        return 2

    def _get_col_count_filter_value(self):
        return 2

    def _get_col_count_final_balance_name(self):
        return 5

    def _get_col_pos_final_balance_label(self):
        return 5

    def _generate_report_content(self, workbook, report):
        # Display array header for item lines
        self.write_array_header()
        # For each product
        for product in report.product_ids:
            # Write product title
            # self.write_array_title(product.product_name)

            # For each item
            for items in product.product_item_ids:

                # Display lines
                self.write_line(items)

            # # 2 lines break
            # self.row_pos += 2

    def write_ending_balance(self, my_object, type_object):
        """Specific function to write ending balance for Open Items"""
        if type_object == 'product':
            name = my_object.product_name
            label = _('Product ending balance')
        elif type_object == 'total':
            name = ''
            label = _(u'总计')
        super(InvoiceItemsCustomerXslx, self).write_ending_balance(my_object, name, label)


InvoiceItemsCustomerXslx(
    'report.account_invoice_extend.report_invoice_items_customer_xlsx',
    'report.invoice.xls',
    parser=report_sxw.rml_parse
)
